﻿/**
 * Copyright (c) 2008 - TheNittyGritty - Thomas Brekelmans
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package nl.thenittygritty.utils.unit
{

	/**
	 * Dimension mimics the flash.geom.Point class but is intended for storing
	 * dimensional values (i.e. width and height values). 
	 */
	public class Dimension
	{
		private var _w:Number = 0;
		private var _h:Number = 0;
		private var _area:Number = 0;

		/**
		 * Creates a new Dimension object with a given width and height.
		 */
		public function Dimension(w:Number, h:Number)
		{
			this.w = w;
			this.h = h;
		}

		/**
		 * Returns a new Dimension object with a width and height of this 
		 * dimension's plus the given dimension's width and height.
		 */
		public function add(dimension:Dimension):Dimension
		{
			return new Dimension(this.w + dimension.w, this.h + dimension.h);
		}

		/**
		 * Returns a new Dimension object with a width and height of this 
		 * dimension's minus the given dimension's width and height.
		 */
		public function subtract(dimension:Dimension):Dimension
		{
			return new Dimension(this.w - dimension.w, this.h - dimension.h);
		}

		/**
		 * Returns a new Dimension object with a width and height of this 
		 * dimension's divided by the given dimension's width and height.
		 */
		public function divide(dimension:Dimension):Dimension
		{
			return new Dimension(this.w / dimension.w, this.h / dimension.h);
		}

		/**
		 * Returns a new Dimension object with a width and height of this 
		 * dimension's width and height divided by a given factor.
		 */
		public function divideBy(factor:Number):Dimension
		{
			return new Dimension(this.w / factor, this._h / factor);
		}

		/**
		 * Returns a new Dimension object with a width and height of this 
		 * dimension's width and height multiplied by the given dimension's width and height.
		 */
		public function multiply(dimension:Dimension):Dimension
		{
			return new Dimension(this.w * dimension.w, this.h * dimension.h);
		}

		/**
		 * Returns a new Dimension object with a width and height of this 
		 * dimension's width and height multiplied by a given factor.
		 */
		public function multiplyBy(factor:Number):Dimension
		{
			return new Dimension(this.w * factor, this._h * factor);
		}

		/**
		 * Returns a new Dimension object with a width and height of this 
		 * dimension's width and height scaled by a given percentage.
		 */
		public function scale(percentage:Number):Dimension
		{
			return new Dimension(this.w / 100 * percentage, this.h / 100 * percentage);
		}

		/**
		 * Returns the width of this dimension.
		 */
		public function get w():Number
		{
			return this._w;
		}

		/**
		 * Changes the width of this dimension to the given number and 
		 * automatically recalculates the area.
		 */
		public function set w(w:Number):void
		{
			this._w = w;
			this._area = this.w * this.h;
		}

		/**
		 * Returns the height of this dimension.
		 */
		public function get h():Number
		{
			return this._h;
		}

		/**
		 * Changes the height of this dimension to the given number and 
		 * automatically recalculates the area.
		 */
		public function set h(h:Number):void
		{
			this._h = h;
			this._area = this.w * this.h;
		}

		/**
		 * Returns the area of this dimension (i.e. the width times the height).
		 */
		public function get area():Number
		{
			return this._area;
		}

		/**
		 * Returns the name of the class, including it's width and height, 
		 * in a String.
		 */
		public function toString():String
		{
			return "Dimension[" + this.w + ", " + this.h + "]";
		}
	}
}